package com.example.httpstest;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.SecureRandom;
import java.util.ArrayList;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class HttpsClientActivity extends Activity {
	private Button testButton;
	private String httpsUrl = "https://app-test.fisc.com.tw/smartpay/constant/constant_lastModify.xhtml";
	HttpClient hc = new DefaultHttpClient();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		testButton = (Button) findViewById(R.id.testButton);
		testButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new Thread(){
					public void run() {
						GetHttps();
//						String result = "";
//						result = HttpFactory.Request(httpsUrl, new ArrayList<NameValuePair>(), null);
//						Log.i("test",result  );
					};
				}.start();
			}
		});
//		testButton.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				try {
//					initKey();
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//				try {
//					String result = getData(httpsUrl);
//					System.out.println("result=" + result);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
	}
	private void GetHttps(){  
//        String https = "https://www.google.com";  
        String https = "https://app-test.fisc.com.tw/smartpay/constant/constant_lastModify.xhtml";  
        try{  
            SSLContext sc = SSLContext.getInstance("TLS");  
            sc.init(null, new TrustManager[]{new MyTrustManager()}, new SecureRandom());  
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());  
            HttpsURLConnection.setDefaultHostnameVerifier(new MyHostnameVerifier());  
            HttpsURLConnection conn = (HttpsURLConnection)new URL(https).openConnection();  
            conn.setDoOutput(true);  
            conn.setDoInput(true);  
            conn.connect();  
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));   
            StringBuffer sb = new StringBuffer();   
            String line;   
            while ((line = br.readLine()) != null)   
                sb.append(line);   
            Log.i("test", sb.toString());
           }catch(Exception e){  
                Log.e(this.getClass().getName(), e.getMessage());  
           }        
     }  
  
  
  
      private class MyHostnameVerifier implements HostnameVerifier{  
            @Override  
            public boolean verify(String hostname, SSLSession session) {  
                    // TODO Auto-generated method stub  
                    return true;  
            }  
  
       }  
  
       private class MyTrustManager implements X509TrustManager{  
            public void checkClientTrusted(X509Certificate[] chain, String authType)  
                            throws CertificateException {  
                    // TODO Auto-generated method stub    
            }  
            public void checkServerTrusted(X509Certificate[] chain, String authType)  
  
                            throws CertificateException {  
                    // TODO Auto-generated method stub      
            }  
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
                    // TODO Auto-generated method stub  
                    return null;  
            }
			@Override
			public void checkClientTrusted(
					java.security.cert.X509Certificate[] chain, String authType)
					throws java.security.cert.CertificateException {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void checkServerTrusted(
					java.security.cert.X509Certificate[] chain, String authType)
					throws java.security.cert.CertificateException {
				// TODO Auto-generated method stub
				
			}          
  
      }     
}  